//登录相关的请求
import request from "@/utils/request";
import {AxiosResponse} from "axios";

//通过枚举管理登录模块的接口地址
enum RequestPath {
    baseUrl = "/api",
    //获取验证码的接口地址
    GET_CAPTCHA_URL = baseUrl + "/login/getcaptcha",
    Login_url = baseUrl + "/login/captcha"
}

interface Result<T> {
    code: number,
    message: string,
    data: T,
}

//获取验证码的数据
export const getCaptcha = async (phone: String) => {
    try {
        // 懒得用实体类了,应该要定义接口的
        let data: Result<string> = await request({
            method: "get",
            url: RequestPath.GET_CAPTCHA_URL,
            params: {
                phoneNumber: phone,
            }
        })
        if (data.code == 200) {
            return;
        } else
            throw new Error("获取验证码失败")
    } catch (e) {
        console.log(e.message)
    }
}
export const login = async (phone: string, captcha: string) => {
    try {
        let res: Result<any> = await request({
            url: RequestPath.Login_url,
            method: "POST",
            data: {
                phoneNumber: phone,
                captcha
            }
        });
        if (res.code == 200) {

            return res.data;
        } else {
            throw new Error("登陆失败")
        }
    } catch (e) {
        console.log(e.message)
    }

}
/*获取小程序登录的二维码*/
export const getQrcode = async () => {
    try {
        let res: any = await request({

            url: "/api/weixin/qrcode",
            method: "get"
        })
        // console.log(res)
        if (res.code === 0) {
            return res.data;
        } else {
            throw new Error(res.message);
        }

    } catch (e) {
        console.log(e.message + "getQrcode")
    }
}
/*获取通过授权后的用户信息*/
export const getWeixinToken = async (tempUserId: String) => {
    try {
        let res:any = await request({

            method: "get",
            url: "/api/weixin/token",
            params: {
                tempUserId,
            }
        })
        if(res.code===200){
            console.log(res.data    )
            return res.data as string;

        }else {
            throw new Error(res.message);
        }

        console.log(res,tempUserId);
    } catch (e) {

        console.log(e.message)
    }

}


